import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:get/get.dart';
import 'package:ljy_p_app/config/config.dart';
import 'package:ljy_p_app/diffwidget/ljy_flex_button.dart';
import 'package:ljy_p_app/local/local_storage.dart';
import 'package:ljy_p_app/localization/localization_key.dart';
import 'package:ljy_p_app/pages/issue/issue_edit_dialog.dart';
import 'package:ljy_p_app/style/lj_style.dart';
import 'package:ljy_p_app/utils/navigator_utils.dart';

typedef StringList = List<String>;

class CommonUtils {
  static List<Color> getThemeListColor() {
    return [
      LJColors.primarySwatch,
      Colors.brown,
      Colors.blue,
      Colors.teal,
      Colors.amber,
      Colors.blueGrey,
      Colors.deepOrange,
    ];
  }

  /// 展示加载弹窗
  static Future<void> showLoadingDialog(BuildContext context) {
    return NavigatorUtils.showLJYDialog(
        context: context,
        builder: (BuildContext context) {
          return Material(
              color: Colors.transparent,
              child: PopScope(
                canPop: false,
                child: Center(
                  child: Container(
                    width: 200.0,
                    height: 200.0,
                    padding: const EdgeInsets.all(4.0),
                    decoration: const BoxDecoration(
                      color: Colors.transparent,
                      //用一个BoxDecoration装饰器提供背景图片
                      borderRadius: BorderRadius.all(Radius.circular(4.0)),
                    ),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        const SpinKitCubeGrid(color: LJColors.white),
                        Container(height: 10.0),
                        Text(LocalizationKey.loadingText.tr,
                            style: LJConstant.normalTextWhite),
                      ],
                    ),
                  ),
                ),
              ));
        });
  }

  /// 问题反馈弹窗
  static Future<void> showEditDialog(
    BuildContext context,
    String dialogTitle,
    ValueChanged<String>? onTitleChanged,
    ValueChanged<String> onContentChanged,
    VoidCallback onPressed, {
    TextEditingController? titleController,
    TextEditingController? valueController,
    bool needTitle = true,
    String? hintText,
  }) {
    return NavigatorUtils.showLJYDialog(
        context: context,
        builder: (BuildContext context) {
          return Center(
            child: IssueEditDialog(
                dialogTitle, onTitleChanged, onContentChanged, onPressed,
                titleController: titleController,
                valueController: valueController,
                needTitle: needTitle,
                hintText: hintText),
          );
        });
  }

  /// 选择语言弹窗
  static showLanguageDialog(BuildContext context) {
    /// 语言列表
    StringList list = [
      LocalizationKey.homeLanguageDefault.tr,
      LocalizationKey.homeLanguageZh.tr,
      LocalizationKey.homeLanguageEn.tr,
      LocalizationKey.homeLanguageKo.tr,
      LocalizationKey.homeLanguageJa.tr,
    ];

    Map<String, Map<String, String>> languageInfo = {
      LocalizationKey.homeLanguageDefault.tr: {
        'languageCode': 'zh',
        'countryCode': 'CN'
      },
      LocalizationKey.homeLanguageZh.tr: {
        'languageCode': 'zh',
        'countryCode': 'CN'
      },
      LocalizationKey.homeLanguageEn.tr: {
        'languageCode': 'en',
        'countryCode': 'US'
      },
      LocalizationKey.homeLanguageKo.tr: {
        'languageCode': 'ko',
        'countryCode': 'KR'
      },
      LocalizationKey.homeLanguageJa.tr: {
        'languageCode': 'ja',
        'countryCode': 'JP'
      },
    };

    CommonUtils.showCommitOptionDialog(context, list, (index) {
      String languageKey = list[index];
      Map<String, String> languageMap = languageInfo[languageKey] ??
          {'languageCode': 'zh', 'countryCode': 'CN'};
      String languageCode = languageMap['languageCode'] ?? 'zh';
      String countryCode = languageMap['countryCode'] ?? 'CN';
      var locale = Locale(languageCode, countryCode);
      Get.updateLocale(locale);
      LocalStorage.save(Config.LOCALE, index.toString());
    }, height: 300.0);
  }

  void changeLanguage(String languageCode, String countryCode) {
    var locale = Locale(languageCode, countryCode);
    Get.updateLocale(locale);
  }

  ///列表item dialog
  static Future<void> showCommitOptionDialog(
      BuildContext context, List<String?>? commitMaps, ValueChanged<int> onTap,
      {width = 250.0, height = 400.0, List<Color>? colorList}) {
    return NavigatorUtils.showLJYDialog(
        context: context,
        builder: (BuildContext context) {
          return Center(
            child: Container(
              width: width,
              height: height,
              padding: EdgeInsets.all(4.0),
              margin: EdgeInsets.all(20),
              decoration: BoxDecoration(
                  color: LJColors.white,
                  borderRadius: BorderRadius.all(Radius.circular(4.0))),
              child: ListView.builder(
                  itemCount: commitMaps?.length ?? 0,
                  itemBuilder: (context, index) {
                    return LjyFlexButton(
                      maxLines: 1,
                      mainAxisAlignment: MainAxisAlignment.start,
                      fontsize: 14.0,
                      color: colorList != null
                          ? colorList[index]
                          : Theme.of(context).primaryColor,
                      textColor: LJColors.white,
                      text: commitMaps![index],
                      onPress: () {
                        Navigator.pop(context);
                        onTap(index);
                      },
                    );
                  }),
            ),
          );
        });
  }

    static String? removeTextTag(String? description) {
    if (description != null) {
      String reg = "<g-emoji.*?>.+?</g-emoji>";
      RegExp tag = RegExp(reg);
      Iterable<Match> tags = tag.allMatches(description);
      for (Match m in tags) {
        String match = m
            .group(0)!
            .replaceAll(RegExp("<g-emoji.*?>"), "")
            .replaceAll(RegExp("</g-emoji>"), "");
        description = description!.replaceAll(RegExp(m.group(0)!), match);
      }
    }
    return description;
  }
}
